package main

import (
	"context"
	"io/ioutil"
	"log"
	"time"

	"github.com/chromedp/chromedp"
)

func main() {
	var buf []byte
	ctx, cancel := chromedp.NewContext(context.Background(), chromedp.WithLogf(log.Printf))
	defer cancel()
	ctx, cancel = context.WithTimeout(ctx, 15*time.Second)
	defer cancel()
	var example string
	err := chromedp.Run(ctx,
		chromedp.Navigate(`https://www.baidu.com`),
		chromedp.WaitVisible(`#s_side_wrapper`),
		chromedp.SendKeys(`#kw`, `zhangguanzhang`, chromedp.ByID),
		chromedp.Click(`#su`, chromedp.NodeVisible),
		chromedp.Text(`#content_left > result.c-container.new-pmd > h3.t > a`, &example),
		chromedp.CaptureScreenshot(&buf),
	)
	if err != nil {
		log.Fatal(err)
	}
	if err := ioutil.WriteFile("fullScreenshot.png", buf, 0644); err != nil {
		log.Fatal(err)
	}
	log.Printf("example: %s", example)

}
